package com.onlyforfun.exchange.view;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.onlyforfun.exchange.R;
import com.onlyforfun.exchange.domain.TimePrice;
import com.onlyforfun.exchange.listner.OnLongClickRateListner;
import com.onlyforfun.exchange.util.CurrencyConverter;
import com.onlyforfun.exchange.util.MoneyFormat;

public class CurrentPriceArrayAdapter extends ArrayAdapter<TimePrice> {
	private final List<TimePrice> list;
	public List<TimePrice> getList() {
		return list;
	}

	private final FragmentActivity context;
	int layoutResourceId;

	public CurrentPriceArrayAdapter(FragmentActivity context, int layoutResourceId,
			List<TimePrice> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.list = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		TimePriceHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new TimePriceHolder();
			holder.spotPurchasePrice = (TextView) row
					.findViewById(R.id.spotPurchasePrice);
			holder.name = (TextView) row
					.findViewById(R.id.name);
			holder.country = (ImageView) row.findViewById(R.id.country_flag);
			holder.cashPurchasePrice = (TextView) row
					.findViewById(R.id.cashPurchasePrice);
			holder.spotOfferPrice = (TextView) row
					.findViewById(R.id.spotOfferPrice);
			holder.cashOfferPrices = (TextView) row
					.findViewById(R.id.cashOfferPrice);
			holder.publicTime = (TextView) row.findViewById(R.id.publicTime);
			holder.unit = (TextView)row.findViewById(R.id.unit);
			
			row.setTag(holder);
		} else {
			holder = (TimePriceHolder) row.getTag();
		}

		TimePrice timePrice = list.get(position);
		holder.name.setText(timePrice.getName());
		holder.country.setImageResource(CurrencyConverter.getIconByName(timePrice.getName()));
		holder.spotPurchasePrice.setText(MoneyFormat.valueOf(timePrice
				.getSpotPurchasePrice()));
		holder.cashPurchasePrice.setText(MoneyFormat.valueOf(timePrice
				.getCashPurchasePrice()));
		holder.spotOfferPrice.setText(MoneyFormat.valueOf(timePrice
				.getSpotOfferPrice()));
		holder.cashOfferPrices.setText(MoneyFormat.valueOf(timePrice
				.getCashOfferPrice()));
		
		holder.unit.setText("单位：每100" + timePrice.getName());
		
		SimpleDateFormat df = new SimpleDateFormat("M-d H:m发布",
				Locale.getDefault());
		
		holder.publicTime.setText(df.format(timePrice.getPublicTime()));
		OnLongClickListener listener = new OnLongClickRateListner(context, timePrice);
		holder.spotPurchasePrice.setOnLongClickListener(listener);
		holder.cashPurchasePrice.setOnLongClickListener(listener);
		holder.spotOfferPrice.setOnLongClickListener(listener);
		holder.cashOfferPrices.setOnLongClickListener(listener);
		return row;
	}

	static class TimePriceHolder {
		public TextView unit;
		TextView spotPurchasePrice;
		ImageView country;
		TextView cashPurchasePrice;
		TextView name;
		TextView spotOfferPrice;
		TextView cashOfferPrices;
		TextView publicTime;
	}
}
